
import os
import time
import logging

import rpyc
from rpyc.utils import server

import dexen
from dexen.system.base import service
from dexen.system.base.spec import DexenSpec
from dexen.system.server.info import UpdateManager

#===============================================================================
# Constants
#===============================================================================
TASK_DIRNAME = "tasks"

#===============================================================================
# Globals
#===============================================================================
update_mgr = UpdateManager()

#===============================================================================
# The Service Class
#===============================================================================
class NodeManagerService(dexen.Service):
    ALIASES = ["NODE_MANAGER"]

    def on_connect(self):
        pass
    
    def on_disconnect(self):
        pass
    
    def run_schema(self, spec):
        assert isinstance(spec, DexenSpec)
        spec = rpyc.classic.obtain(spec)
        #print "the len schema zip", len(spec.schema_zip), type(spec.schema_zip)
        update_mgr.run_master(spec)

    

def main():
    try:
        logging.basicConfig(level=logging.DEBUG)
        svr = server.ThreadedServer(service=NodeManagerService, 
                                    protocol_config=service.default_config)
        update_mgr.start()
        svr.start()     
    finally:
        pass
    
if __name__ == '__main__':
    main()

